package il.ac.tau.cvws.scrabblesidekick.client.ui;

import il.ac.tau.cvws.scrabblesidekick.client.app.controller.CameraController;
import il.ac.tau.cvws.scrabblesidekick.client.ui.service.UIServices;
import il.ac.tau.cvws.scrabblesidekick.client.ui.view.CameraLayoutView;
import il.ac.tau.cvws.scrabblesidekick.client.ui.view.camera.CameraState;
import il.ac.tau.cvws.scrabblesidekick.client.ui.view.dialog.ProgressDialogHandler;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

/* Controls the camera view and interactions. When the activity loses or regains
 * focus, the camera needs to be handled accordingly, etc. The activity also handles
 * success and failure of the acquiring of images.
 */
public abstract class CameraActivity extends ScrabbleSidekickActivity 
{
	protected LinearLayout 	        _layout;
	protected CameraLayoutView      _cameraView;
	protected CameraState	        _state;
	protected ProgressDialogHandler _handler;
	protected Context 				_context;
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		_context = this;
		_state   = CameraState.OFF;
		_handler = new ProgressDialogHandler();
				
		_layout = new LinearLayout(this);
		_layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		
		setContentView(_layout);
	}
	
	/* Resume the camera preview if the activity resumes.
	 */
	@Override
 	public void onResume() 
 	{
 		super.onResume();
    
 		if (_state == CameraState.ON)
 		{
 			_cameraView.refreshView();
 			_cameraView.restart();
 		}
 	}
    
	/* Stop the preview if the activity pauses.
	 */
	@Override
	public void onPause() 
	{
		if (_state == CameraState.ON)
 		{
 			_cameraView.closeView();
 			
 			_state = CameraState.OFF;
 		}
          
		super.onPause();
	}
	
	/* Stop the preview if the activity stops.
	 */
	@Override
	public void onStop() 
	{
		if (_state == CameraState.ON)
 		{
 			_cameraView.closeView();
 			
 			_state = CameraState.OFF;
 		}
          
		super.onPause();
	}
	
	/* Stop the preview if the user asks to exit.
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
	    if ((keyCode == KeyEvent.KEYCODE_BACK))
	    {
	        onStop();
	    }
	    
	    return super.onKeyDown(keyCode, event);
	}
	
	/* Create the camera view and lay it on the parent layout.
	 */
	protected void buildCameraView()
	{
		_cameraView = new CameraLayoutView(
				(CameraController)_controller,
				this, _handler, _screenWidth, _screenHeight);
		
		_layout.addView(_cameraView);		
	}
	
	/* Delegate failure to the concrete activity, on the UI thread.
	 */
	public void handleFailure(final String msg)
	{
		new Runnable()
		{
			@Override
			public void run()
			{
				runOnUiThread(new Runnable()
				{
					@Override
					public void run()
					{
						innerHandleFailure(msg);
					}
				});
			}
		}.run();
	}

	/* Delegate success to the concrete activity, on the UI thread.
	 */
	public void handleSuccess()
	{
		new Runnable()
		{
			@Override
			public void run()
			{
				runOnUiThread(new Runnable()
				{
					@Override
					public void run()
					{
						innerHandleSuccess();
					}
				});
			}
		}.run();
	}
	
	/* Display an error message and hide the current progress bar.
	 */
	public void innerHandleFailure(String msg)
	{
		_handler.hideProgressBar();
		
		UIServices.showDialog(_context, "ERROR", msg, new OnCancelListener()
		{
			@Override
			public void onCancel(DialogInterface dialog)
			{
				finish();
			}
		});		
	}
	
	/* Success delegation.
	 */
	public abstract void innerHandleSuccess();
}
